<template>
  <div class="avue-contail" :class="{'avue--collapse':isCollapse}">
    <otherLoginCheck></otherLoginCheck>
    <div class="avue-header">
      <!-- 顶部导航栏 -->
      <top />
    </div>

      <div class="avue-layout" :style="layoutStyle">
        <div class="avue-left" v-if='avueleftShow'>
          <!-- 左侧导航栏 -->
          <sidebar />
        </div>
        <div class="avue-main">
          <!-- 顶部标签卡 -->
          <tags class="title-tags" v-if='avueleftShow'/>
          <!-- 主体视图层 -->
          <!-- <el-scrollbar style="height:100%"> -->
            <keep-alive>
              <router-view class="avue-view" v-if="$route.meta.keepAlive" />
            </keep-alive>
            <router-view class="avue-view" v-if="!$route.meta.keepAlive" />
          <!-- </el-scrollbar> -->
        </div>
      </div>
    <!-- <el-footer class="avue-footer">
      <img src="/svg/logo.svg"
           alt=""
           class="logo">
      <p class="copyright">© 2018 Avue designed by smallwei</p>
    </el-footer> -->
    <div class="avue-shade" @click="showCollapse"></div>
  </div>
</template>

<script>
  import {
    mapGetters
  } from "vuex";
  import tags from "./tags";
  import top from "./top/";
  import sidebar from "./sidebar/";
  import admin from "@/util/admin";
  import {
    validatenull
  } from "@/util/validate";
  import {
    calcDate
  } from "@/util/date.js";
  import {
    getStore,
    setStore
  } from "@/util/store.js";
  import otherLoginCheck from '@/components/login/otherLoginCheck'
  let lastOperateTime = new Date()
  import {getToken} from '@/util/auth'

  export default {
    components: {
      top,
      tags,
      sidebar,
      otherLoginCheck
    },
    name: "index",
    data() {
      return {
        //刷新token锁
        refreshLock: false,
        //刷新token的时间
        refreshTime: "",
        deptIds: '',
        avueleftShow: true,
        avuemain:'index',
        refreshRateTime: 15 * 60 * 1000, // 刷新token一次的时间区间
      };
    },
    created() {
      const lastRefreshTokenTime = getStore({name: 'lastRefreshTokenTime'})
      if (lastRefreshTokenTime) {// 根据缓存时间刷新token
        const gapTime = new Date().getTime() - lastRefreshTokenTime
        if (gapTime >= this.refreshRateTime) {
          this.refreshToken()
        }
      }
      //实时检测刷新token
      this.setRefreshTokenTimer();
      this.setOperateListener();
      this.initData();//预加载数据
    },
    mounted() {
      this.deptIds = this.$route.query.cityId;
      this.init();
    },
    computed: {
      ...mapGetters(["isLock", "isCollapse", "website"]),
      layoutStyle() {
        if (this.avueleftShow) {
          if (this.isCollapse) {
            return {
              'margin-left': '64px',
              'width': 'calc(100% - 64px)',
            }
          } else {
            return {
              'margin-left': '200px',
              'width': 'calc(100% - 200px)',
            }
          }
        } else {
          return {
            'margin-left': '0px',
            'width': '100%',
          }
        }
      }
    },
    props: [],
    methods: {
      showCollapse() {
        this.$store.commit("SET_COLLAPSE");
      },
      // 屏幕检测
      init() {
        const hideLeftMenu = this.$route.meta && this.$route.meta.hideLeftMenu
        this.avueleftShow = !hideLeftMenu
        this.$store.commit("SET_SCREEN", admin.getScreen());
        window.onresize = () => {
          setTimeout(() => {
            this.$store.commit("SET_SCREEN", admin.getScreen());
          }, 0);
        };
      },
      setRefreshTokenTimer() {
        this.refreshTime = setInterval(() => {
          const nowTime = new Date().getTime()
          const operateGapTime = nowTime - lastOperateTime.getTime() //当前时间和用户最后一次操作时间差
          const lastRefreshTokenTime = getStore({name: 'lastRefreshTokenTime'})
          const refreshGapTime = nowTime - (lastRefreshTokenTime || 0);
          if (operateGapTime <= this.refreshRateTime/** 在刷新时区内有操作 */ &&
              refreshGapTime >= this.refreshRateTime/** 上次刷新时间大于刷新时区 */) {
            this.refreshToken()
          }
        }, 30 * 1000);
      },
      refreshToken() {
        if (!this.refreshLock) {
          console.log('更新token')
          this.refreshLock = true;
          this.$store
            .dispatch("RefeshToken").then(res => {
              setStore({name: 'lastRefreshTokenTime', content: new Date().getTime()})
            }).finally(() => {
              this.refreshLock = false;
            });
        }
      },
      clearTime() {
        this.refreshTime && clearInterval(this.refreshTime)
        this.refreshTime = null
      },
      setOperateListener() {
        document.addEventListener('click', () => {
          lastOperateTime = new Date()
        })
      },
      initData() {
        if(getToken()) {
          this.$store.dispatch('getAddedServicesTypeList')
        }
      }
    },
    destroyed() {
      this.clearTime()
    }
  };
</script>
